package com.eam.handler;

import com.eam.data.EamSpareparts;
import com.eam.data.EamSparepartsInventory;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.service.OBDal;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 设备备品配件物料清单盘点审核
 * 
 */
public class SparepartsResultHandler extends BaseActionHandler {
	

	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject jsonRep = new JSONObject();
		JSONObject meg = new JSONObject();
		try {
			JSONObject reqData = new JSONObject(data);
			
			String resultId = reqData.getString("id");  //获取行ID
			String status = reqData.getString("type");  //获取盘点状态
			String pdqtys = reqData.getString("pdqtys");  //获取盘点数量
			String kcqtys = reqData.getString("kcqtys");  //获取库存数量
			
			String pdresult = "normal";  //盘点状态
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置日期格式
			String dates = df.format(new Date());
			
			String sql = "select Eam_Spareparts_Inventory_id from Eam_Spareparts_Inventory pd where pd.created = (select max(created) from Eam_Spareparts_Inventory where Eam_Spareparts_id = ? )";
			
			String pdids = (String)OBDal.getInstance().getSession().createSQLQuery(sql).setString(0, resultId).uniqueResult();
			
			EamSpareparts wl = OBDal.getInstance().get(EamSpareparts.class, resultId);  //获取选中的物料清单
			
			EamSparepartsInventory pd = OBDal.getInstance().get(EamSparepartsInventory.class, pdids);  //获取选中的物料清单的盘点记录
			
			BigDecimal kunumbers = new BigDecimal(pdqtys);   //盘点数量存库存数量
			
			if(status.equals("passed")){
				//wl.setNumbers(kunumbers);
				 wl.setProfitLossNumbers(null);
				 wl.setProfitLossStatus(null);
				 wl.setStatus(null);
				 pd.setSparefield("passed");
			}else if(status.equals("Notthrough")){
				 wl.setProfitLossNumbers(null);
				 wl.setProfitLossStatus(null);
				 wl.setStatus(null);
				 pd.setSparefield("Notthrough");
			}
			
			OBDal.getInstance().save(wl);
			OBDal.getInstance().save(pd);
			OBDal.getInstance().commitAndClose();
			
		}catch (Exception e) {
			OBDal.getInstance().rollbackAndClose();
			e.printStackTrace();
		}
		return meg;
	}

	
}
